package com.jiangjiesheng.shiro.realm;

import com.jiangjiesheng.dao.UserDao;
import com.jiangjiesheng.vo.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 自定义Realm
 */
public class CustomRealm extends AuthorizingRealm {

    //    Map<String, String> userMap = new HashMap<String, String>(16);
//
//    public static void main(String[] args) {
//        System.out.println(new Md5Hash("123456", "salt").toString());//e10adc3949ba59abbe56e057f20f883e
//    }
    public static void main(String[] args) {
        System.out.println(new Md5Hash("123456", "js").toString());//ad0c1f9a32f1b60f2154c76733f14cd0
    }

    //
//    {   // f51703256a38e6bab3d9410a070c32ea 加盐前
//        // e10adc3949ba59abbe56e057f20f883e 加盐后
//        userMap.put("js", "f51703256a38e6bab3d9410a070c32ea");// 123456 使用Shiro加密后的密码，实际应该是数据库中的
//        //super.setName("customRealmName"); // 似乎不设置也没有问题
//    }
    @Autowired
    private UserDao userDao;

    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = (String) principalCollection.getPrimaryPrincipal();
        Set<String> roles = getRolesByUserName(userName);
        Set<String> permissions = getPermissionsByUserName(userName);//可以通过 roles 这个来遍历List查询
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);
        return simpleAuthorizationInfo;
    }

    // 认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 1. 从主体传过来的认证信息中，获取用户名
        String userName = (String) authenticationToken.getPrincipal();
        // 2. 通过用户名到数据库中获取凭证
        String password = getPasswordByUserName(userName);
        // 不要直接通过用户名和密码去查询，这样不好判断是密码错误还是用户名不存在
        // 通过select password的方式，可以判断这两种情况
        if (password == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo =
                new SimpleAuthenticationInfo(userName, password, "customRealmName");

        //有加盐时
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(userName));

        return authenticationInfo;
    }

    private String getPasswordByUserName(String userName) {
        //模拟读取数据裤
        // return userMap.get(userName);
        System.out.println("从数据库中读取用户数据");
        User user = userDao.getUserByUserName(userName);
        if (user != null) {
            return user.getPassWord();
        }
        return null;
    }

    private Set<String> getRolesByUserName(String userName) {
        //从数据库或者缓存中获取角色数据
        //  Set<String> sets = new HashSet<String>();
        //  sets.add("admin");
        //  sets.add("user");
        List<String> list = userDao.queryRolesByUserName(userName);
        if (list != null) {
            Set<String> sets = new HashSet<>(list);
            return sets;
        }
        return null;
    }

    /*
     * 这个地方是不是应该ByRoleName更合适？
     */
    private Set<String> getPermissionsByUserName(String userName) {
        //从数据库或者缓存中获取角色数据
//        Set<String> sets = new HashSet<String>();
//        sets.add("user:delete");
//        sets.add("user:add");
        List<String> list = userDao.queryPermissionsByUserName(userName);
        if (list != null) {
            Set<String> sets = new HashSet<>(list);
            return sets;
        }
        return null;

    }
}
